home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 54 < prev    next >
Internet Message Format  |  1996-08-06  |  7KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.std.c
  4. Subject: Re: Undefined result vs. int's holding undefined values.
  5. Date: Tue, 09 Jan 96 02:37:54 GMT
  6. Organization: none
  7. Message-ID: <821155074snz@genesis.demon.co.uk>
  8. References: <4ck70b$rd7@news.informix.com> <4ckms5$rd7@news.informix.com> <4cmg0s$1mb@der.twinsun.com> <oZA8wQ9ytpjN084yn@csn.net> <4cs460$d6e@news.informix.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4cs460$d6e@news.informix.com>
  15.            dwood@informix.com "Daniel Wood" writes:
  16.  
  17. >thads@csn.net (Thad Smith) wrote:
  18. >>In article <4cmg0s$1mb@der.twinsun.com>,
  19. >>eggert@twinsun.com (Paul Eggert) wrote:
  20. >>>This reminds me of a similar bug I found a long time ago when porting
  21. >>>the Modula-3 runtime, which contained code that acted something like this:
  22. >>>
  23. >>>      int sum_overflow (int x, int y) {
  24. >>>              return (x + y < x) != (y < 0);
  25. >>>      }
  26. >>>
  27. >>>The C Standard does not guarantee that the above function works,
  28. >>>since integer overflow leads to undefined behavior,
  29. >>>but when I found that the function did not work with whatever old version
  30. >>>of GCC I was using at the time, I reported it as a bug to the GCC maintainers
  31. >>>and got a fix from them in a few days.
  32. >>>
  33. >>>Regardless of what the C Standard says, it should be obvious that it's
  34. >>>crucial to have integer overflow checking working properly in an
  35. >>>application that needs it.  
  36. >>
  37. >>I agree, but it is possible to rewrite the function so that it doesn't
  38. >>invoke undefined behavior:
  39. >>
  40. >>  #include <limits.h>
  41. >>  int sum_overflow (int x, int y) {
  42. >>      return x > 0? (y > INT_MAX - x) : (y < INT_MIN - x);
  43. >>  }
  44. >>
  45. >>Thad
  46. >
  47. >I totally understand what you are doing in the above but this would have to
  48. >be the ultimate in a cheap out for a vendor.
  49.  
  50. Rubbish. A standard only works if all parties stick to it. A program which
  51. overflows signed integer arithmetic is plain broken and no vendor has the
  52. responsibility to support it, any more than they, for instance, have to
  53. support arbitrary casting between ints and pointers. Historically there is
  54. code that has relied on that but as time progresses it get fixed.
  55.  
  56. >SCO could claim that before
  57. >ever looking at a test case containing a suspected compiler bug that every
  58. >arithmetic calculation would have to first have a test similar to the above
  59. >to protect against overflow/underflow.
  60.  
  61. What SCO claim in that case doesn't matter. Either the program does
  62. invoke undefined behaviour according to the standard or it does not. There
  63. are many ways a program can invoke undefined behavior, and many of those
  64. not always trivial to prove or disprove in a particular program. You might
  65. just as well say that a program would have to test against NULL every time
  66. it dereferences a pointer. Your argument also applies to floating point
  67. arithmetic.
  68.  
  69. > Does an appropriate "SAFE TEST" exist
  70. >for multiple.  Has anyone actually seen a real production program where every
  71. >calculation was protected against overflow/underflow.
  72.  
  73. In most instances the ranges of the variables concerned are known and hence
  74. it can be guaranteed that a signed integer operation won't overflow. If it
  75. does you have a bug in the program or are depending on behaviour which the
  76. C language doesn't guarantee and which doesn't work on some systems for
  77. good reasons.
  78.  
  79. >Shame on SCO for using such a cheap out.
  80.  
  81. The compiler I saw this on was icc which is the Intel Reference Compiler
  82. (which SCO supply, but is controlled by Intel). The stock SCO compiler
  83. probably satisfies your wishes. The Intel compiler is designed purely and
  84. simply for speed and it does some very hairy optimisations. The bottom line
  85. is that if it compiles strictly conforming code it is a conforming C
  86. compiler and 100% correct. If Intel managed to squeeze extra speed out by
  87. not supporting the 'normal' form of undefined behavour then full marks to
  88. them - it would be silly to slow the generated code down to support illegal
  89. source code. (I don't think anybody ever accused Intel of a 'cheap out'
  90. on their compiler before!)
  91.  
  92. > There is no reason on an intel based
  93. >platform not to be able to create an "IMPLEMENTATION DEFINED and consistant"
  94. >behavior implementation instead of undefined behavior.
  95.  
  96. It is the C standard that makes the behaviour undefined, not the
  97. implementation. If the behaviour you desire were so essential it would have
  98. been incorporated into the standard. In fact the C standard defines this
  99. sort of behaviour for unsigned types, so if you need it use those. I have
  100. yet to encounter a program that really needs well defined signed integer
  101. overflow behaviour and can't be done simply another way.
  102.  
  103. > Granted the standard
  104. >doesn't require it but I have never seen a program with the kind of extra
  105. >checking that seems to be required.
  106.  
  107. And even with well-defined integer overflow behaviour most programs would
  108. fail/generate garbage results if overflow occurred, so it doesn't really
  109. help.
  110.  
  111. >Integer overflow/wraparound producing some
  112. >specific defined behavior is easily "doable" on all machine architectures I
  113. >know of even if the results might differ on different machines.
  114.  
  115. If you can't get consistent results on different machines it is rather
  116. pointless.
  117.  
  118. >Do any machines exist which actually explode when you add two number together
  119. >such that the result would exceed MAXINT? :-)  Get pratical!
  120.  
  121. One possible behaviour is to generate an exception on integer
  122. overflow and some archetectures can support this.
  123.  
  124. >I am particularly interested in the answer to my "safe multiply" question
  125. >above.  It would be quite funny to find that there is actually no way to
  126. >create, in a practical way, a safe c program that used multiple if the
  127. >standard was followed to the letter of the law.
  128.  
  129. In the general case this is quite difficult in any language that
  130. doesn't have multiple-precision support. There are various ways you could
  131. tackle it e.g. one analogous to the sum example, or using floating point.
  132. It may be easier if the test can fail on values close to INT_MIN and INT_MAX,
  133. or the code assumes that INT_MIN is -INT_MAX (such an assumption may help
  134. portability by, for example, only allowing the ranges guaranteed by the
  135. standard).
  136.  
  137. -- 
  138. -----------------------------------------
  139. Lawrence Kirby | fred@genesis.demon.co.uk
  140. Wilts, England | 70734.126@compuserve.com
  141. -----------------------------------------
  142.